package com.mall.user.services;

import com.alibaba.fastjson.JSON;
import com.mall.user.UserService;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.dal.persistence.MemberMapper;
import com.mall.user.dal.persistence.UserVerifyMapper;
import com.mall.user.dto.*;
import com.mall.user.utils.ExceptionProcessorUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

/**
 * @author 杨杰
 * @version V1.0
 * @Package com.mall.user.services
 * @date 2020/12/19 15:48
 * @Copyright © 暂时还没有
 * @description
 */
@Slf4j
@Component
@Service(interfaceClass = UserService.class)
public class UserServiceImp implements UserService {

    @Autowired
    MemberMapper memberMapper;
    @Autowired
    UserVerifyMapper userVerifyMapper;
    @Override
    public UserLoginResponse userLogin(UserLoginRequest request) {
        UserLoginResponse loginResponse = null;


        String password = request.getPassword();
        String userName = request.getUserName();
        String pwd = DigestUtils.md5DigestAsHex(password.getBytes());
        System.out.println("----加密密码----"+pwd);

        /*
        检查是否邮箱注册
         */
        String status = userVerifyMapper.getVerifyStatusByNameAndPwd(userName, pwd);
        if (!"Y".equals(status)){
            loginResponse = new UserLoginResponse();
            loginResponse.setCode(SysRetCodeConstants.USER_ISVERFIED_ERROR.getCode());
            loginResponse.setMsg(SysRetCodeConstants.USER_ISVERFIED_ERROR.getMessage());
            return loginResponse;
        }


        //从member表查询基本信息
        loginResponse = memberMapper.loginQuery(userName, pwd);
        if (loginResponse == null){//为null表示账户密码不正确，查询不到信息
            log.info("user账号密码验证失败");
            loginResponse = new UserLoginResponse();
            loginResponse.setCode(SysRetCodeConstants.REQUEST_CHECK_FAILURE.getCode());
            loginResponse.setMsg(SysRetCodeConstants.REQUEST_CHECK_FAILURE.getMessage());
            return loginResponse;
        }
        loginResponse.setCode(SysRetCodeConstants.SUCCESS.getCode());
        loginResponse.setMsg(SysRetCodeConstants.SUCCESS.getMessage());

        //获取token
        UserTokenBean userTokenBean = new UserTokenBean(loginResponse.getId(),loginResponse.getUsername());
        String user_Json = JSON.toJSONString(userTokenBean);
        log.info("用户信息写入token： "+ user_Json);
        //JSON.parseObject(person, Person.class);             //封装用户信息json格式
        JwtTokenServiceImpl jwtTokenUtils = JwtTokenServiceImpl.builder().msg(user_Json).build();
        String token = jwtTokenUtils.creatJwtToken();
        loginResponse.setToken(token);
        log.info("登录校验，生成token数据:" + token);

        return loginResponse;
    }

    @Override
    public UserTokenBean userLogin(String token) {

        //解析token

        log.info("解析token:  {}",token);
        JwtTokenServiceImpl tokenUtil = JwtTokenServiceImpl.builder().token(token).build();
        String userTokenInfo = tokenUtil.freeJwt();
        UserTokenBean userToken = JSON.parseObject(userTokenInfo, UserTokenBean.class);
        System.out.println(userToken);

        return userToken;
    }

    /**
     * @param request
     * @return
     * @author heyyon
     * 先查询出按给定的用户名和uuid的行数
     */
    @Override
    public UserVerifyResponse countByNameAndUid(UserVerifyRequest request) {
        UserVerifyResponse response = new UserVerifyResponse();
        try {
            request.requestCheck();
            int row = userVerifyMapper.countByNameAndUid(request.getUserName(), request.getUuid());
            if (row == 0) {
                response.setCode(SysRetCodeConstants.DATA_NOT_EXIST.getCode());
                response.setCode(SysRetCodeConstants.DATA_NOT_EXIST.getMessage());
            }
            response.setRow(row);
            response.setCode(SysRetCodeConstants.SUCCESS.getCode());
            response.setMsg(SysRetCodeConstants.SUCCESS.getMessage());
        } catch (Exception e) {
            log.error("AddressServiceImpl.addressList occur Exception :"+e);
            ExceptionProcessorUtils.wrapperHandlerException(response,e);
        }
        return response;
    }
    @Transactional
    @Override
    public UserVerifyResponse updateVerifyTagByName(UserVerifyRequest request) {
        UserVerifyResponse response = new UserVerifyResponse();
        try {
            request.requestCheck();
            int row1=memberMapper.updateVerifyTagByName(request.getUserName());
            int row2 = userVerifyMapper.updateVerifyTagByName(request.getUserName());
            if (row1 == 0|row2==0) {
                response.setCode(SysRetCodeConstants.DATA_NOT_EXIST.getCode());
                response.setCode(SysRetCodeConstants.DATA_NOT_EXIST.getMessage());
            }
            response.setCode(SysRetCodeConstants.SUCCESS.getCode());
            response.setMsg(SysRetCodeConstants.SUCCESS.getMessage());
        } catch (Exception e) {
            log.error("AddressServiceImpl.addressList occur Exception :"+e);
            ExceptionProcessorUtils.wrapperHandlerException(response,e);
        }
        return response;
    }
}
